<template>
  <div class="cart">
    <h4>全部商品</h4>
    <div class="cart-main">
      <div class="cart-th">
        <div class="cart-th1">全部</div>
        <div class="cart-th2">商品</div>
        <div class="cart-th3">单价（元）</div>
        <div class="cart-th4">数量</div>
        <div class="cart-th5">小计（元）</div>
        <div class="cart-th6">操作</div>
      </div>
      <!-- work1 -->
      <template v-if="cartInfoList">
        <div class="cart-body"  v-for="data,index in cartInfoList" :key="data.id">
        <ul class="cart-list">
          <li class="cart-list-con1">
            <input type="checkbox" name="chk_list" :checked="data.isChecked == 1" @change="theselected(index)">
          </li>
          <li class="cart-list-con2">
            <img :src="data.imgUrl">
            <div class="item-msg">{{data.skuName}}</div>
          </li>

          <li class="cart-list-con4">
            <span class="price">{{data.skuPrice}}</span>
          </li>
          <li class="cart-list-con5">
            <a href="#" @click.prevent="cut(index)" class="mins">-</a>
            <input autocomplete="off" type="text" :value="data.skuNum" minnum="1" class="itxt" @blur="thisnumber($event,index)" @keydown.enter="thisnumber($event,index)">
            <a href="#" @click.prevent="add(index)" class="plus">+</a>
          </li>
          <li class="cart-list-con6">
            <span class="sum">{{data.skuPrice * data.skuNum}}</span>
            
          </li>
          <li class="cart-list-con7">
            <a href="#" class="sindelet" @click.prevent="del(data.skuId)">删除</a>
            <br>
            <a href="#">移到收藏</a>
          </li>
        </ul>
      </div>
      </template>
    </div>
    <div class="cart-tool">
      <div class="select-all">
        <input class="chooseAll" type="checkbox" :checked='allslect' @change="allSlectStatic">
        <span>全选</span>
      </div>
      <div class="option">
        <a href="#" @click.prevent="delall">删除选中的商品</a>
        <a href="#none">移到我的关注</a>
        <a href="#none">清除下柜商品</a>
      </div>
      <div class="money-box">
        <div class="chosed">已选择
          <span>{{aggregate}}</span>件商品</div>
        <div class="sumprice">
          <em>总价（不含运费） </em>
          <i class="summoney">{{everysum}}</i>
        </div>
        <div class="sumbtn">
          <router-link to="trade">结算</router-link>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import {debounce} from 'lodash'
import {mapActions,mapState} from 'vuex'
  export default {
    name: 'ShopCart',
    methods:{
      ...mapActions('shoppingcart',['getListShopping','delShoppingList',"checkedListnow"]),
      ...mapActions('datial',['getShopping']),
    async delall(){
      let num = 1
        for(let i = 0;i <= this.cartInfoList.length;i++){
          if(this.cartInfoList[i]){
            if(this.cartInfoList[i].isChecked == 1){
            await this.delShoppingList(this.cartInfoList[i].skuId)
            num ++
          }
          }
        }
        this.getListShopping()
      },
      del(e){
        if(confirm('确定要在购物车移除吗?')){
          try {
            this.delShoppingList(e).then(()=>{this.getListShopping()})
            
          } catch (error) {
            alert('哎呀呀信号强度不够了')
          }
          
        }
      },
      detection:debounce(function(e){
        let srf = /\D*/
        if(e * 1 + '' === 'NaN' || !e){
          this.valueInput = 1
        }else{
          this.valueInput = parseInt(e)
        }
        },1000),
      allSlectStatic(){
          this.cartInfoList.forEach(async name=>{
            if(this.allslect){
              await this.checkedListnow([name.skuId,0])
              name.isChecked = 0
            }else{
              await this.checkedListnow([name.skuId,1])
              name.isChecked = 1
            }
            })
      },
      add(e){
        this.getShopping([this.cartInfoList[e].skuId,1])
        // console.log(this.cartInfoList[e].skuNum)
          this.cartInfoList[e].skuNum ++
      },
      async theselected(e){
        if(this.cartInfoList[e].isChecked == 1){
          await this.checkedListnow([this.cartInfoList[e].skuId,0])
          this.cartInfoList[e].isChecked = 0
        } else{
          await this.checkedListnow([this.cartInfoList[e].skuId,1])
          this.cartInfoList[e].isChecked = 1
        }
      },
      cut(e){
        if(this.cartInfoList[e].skuNum >1){
          this.getShopping([this.cartInfoList[e].skuId,-1])
          this.cartInfoList[e].skuNum --
        }
        // let ref = this.$refs.nemburs 
        // if(ref[e].value > 1 && ref[e].value * 1 + '' !== 'NaN'){
        //   ref[e].value --
        // }
      },
      thisnumber(_,e){
        console.log(_.target.value)
        if(_.target.value * 1 + '' !== 'NaN' && _.target.value > 0){
          if(_.target.value < this.cartInfoList[e].skuNum ){
            this.getShopping([this.cartInfoList[e].skuId,- parseInt((this.cartInfoList[e].skuNum - _.target.value))])
          }else if(_.target.value > this.cartInfoList[e].skuNum){
            this.getShopping([this.cartInfoList[e].skuId,  parseInt(_.target.value - this.cartInfoList[e].skuNum )])
          }
          this.cartInfoList[e].skuNum = parseInt(_.target.value)
        }else{
          _.target.value = this.cartInfoList[e].skuNum 
        }
        _.target.blur()
      }
    },
    computed:{
      aggregate(){
        let num = 0;
        this.cartInfoList.map(name => {
          num += parseInt(name.skuNum)
        })
        return num
      },
      ...mapState('shoppingcart',['shoppingList']),
      cartInfoList(){
        if(this.shoppingList.length !== 0 || ! this.shoppingList){
          return this.shoppingList[0].cartInfoList
        }else{
          return []
        }
      },
      sum(){
        if(this.valueInput * 1 + '' !== 'NaN'){
          return this.valueInput
        }else{
          return 1
        }
      },
      everysum(){
        let sum = 0;
        this.cartInfoList.forEach(element => {
          if(element.isChecked == 1){
            sum += element.skuPrice * element.skuNum
          }
        });
        return sum
      },
      allslect(){
        // let sum = 0;
        // this.cartInfoList.forEach(element => {
        //   if(element.isChecked == 1){
        //     sum ++;
        //   }
        // });
        // if(sum =  this.cartInfoList.length){
        //   return true
        // }else{
        //   return false
        // }
        if(this.cartInfoList.length > 0){
          return this.cartInfoList.every(name => name.isChecked == 1)
        }else{
          return false
        }
        //判断数组中所有的元素是否符合要求
        // console.log(this.cartInfoList.every(name => name.isChecked == 1))
      }
    },
    created(){
      this.getListShopping()
    }
  }
</script>

<style lang="less" scoped>
  .cart {
    width: 1200px;
    margin: 0 auto;

    h4 {
      margin: 9px 0;
      font-size: 14px;
      line-height: 21px;
    }

    .cart-main {
      .cart-th {
        background: #f5f5f5;
        border: 1px solid #ddd;
        padding: 10px;
        overflow: hidden;

        &>div {
          float: left;
        }

        .cart-th1 {
          width: 25%;

          input {
            vertical-align: middle;
          }

          span {
            vertical-align: middle;
          }
        }

        .cart-th2 {
          width: 25%;
        }

        .cart-th3,
        .cart-th4,
        .cart-th5,
        .cart-th6 {
          width: 12.5%;

        }
      }

      .cart-body {
        margin: 15px 0;
        border: 1px solid #ddd;

        .cart-list {
          padding: 10px;
          border-bottom: 1px solid #ddd;
          overflow: hidden;

          &>li {
            float: left;
          }

          .cart-list-con1 {
            width: 15%;
          }

          .cart-list-con2 {
            width: 35%;

            img {
              width: 82px;
              height: 82px;
              float: left;
            }

            .item-msg {
              float: left;
              width: 150px;
              margin: 0 10px;
              line-height: 18px;
            }
          }


          .cart-list-con4 {
            width: 10%;

          }

          .cart-list-con5 {
            width: 17%;

            .mins {
              border: 1px solid #ddd;
              border-right: 0;
              float: left;
              color: #666;
              width: 6px;
              text-align: center;
              padding: 8px;
            }

            input {
              border: 1px solid #ddd;
              width: 40px;
              height: 33px;
              float: left;
              text-align: center;
              font-size: 14px;
            }

            .plus {
              border: 1px solid #ddd;
              border-left: 0;
              float: left;
              color: #666;
              width: 6px;
              text-align: center;
              padding: 8px;
            }
          }

          .cart-list-con6 {
            width: 10%;

            .sum {
              font-size: 16px;
            }
          }

          .cart-list-con7 {
            width: 13%;

            a {
              color: #666;
            }
          }
        }
      }
    }

    .cart-tool {
      overflow: hidden;
      border: 1px solid #ddd;

      .select-all {
        padding: 10px;
        overflow: hidden;
        float: left;

        span {
          vertical-align: middle;
        }

        input {
          vertical-align: middle;
        }
      }

      .option {
        padding: 10px;
        overflow: hidden;
        float: left;

        a {
          float: left;
          padding: 0 10px;
          color: #666;
        }
      }

      .money-box {
        float: right;

        .chosed {
          line-height: 26px;
          float: left;
          padding: 0 10px;
        }

        .sumprice {
          width: 200px;
          line-height: 22px;
          float: left;
          padding: 0 10px;

          .summoney {
            color: #c81623;
            font-size: 16px;
          }
        }

        .sumbtn {
          float: right;

          a {
            display: block;
            position: relative;
            width: 96px;
            height: 52px;
            line-height: 52px;
            color: #fff;
            text-align: center;
            font-size: 18px;
            font-family: "Microsoft YaHei";
            background: #e1251b;
            overflow: hidden;
          }
        }
      }
    }
  }
</style>